<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'

// 获取路由信息
const route = useRoute()
const router = useRouter()

// 视频详情数据
const videoDetail = ref({
  id: 1,
  title: '三毛流浪记：经典动画大片',
  description: '《三毛流浪记》是根据张乐平同名漫画改编的动画作品，讲述了孤儿三毛在旧上海的种种遭遇。本片通过三毛的视角，展现了旧社会的黑暗和人情冷暖，同时也表现了三毛的善良、乐观和坚强。',
  director: '张乐平',
  releaseDate: '1999年',
  duration: '90分钟',
  rating: '9.2',
  cover: '/static/images/image1.jpg',
  videoUrl: '/static/videos/video1.mp4',
  relatedVideos: [
    { id: 2, title: '三毛流浪记经典片段2', cover: '/static/images/image2.jpg' },
    { id: 3, title: '三毛流浪记经典片段3', cover: '/static/images/image3.jpg' },
    { id: 4, title: '三毛流浪记经典片段4', cover: '/static/images/image4.jpg' },
    { id: 5, title: '三毛奇遇记：奇幻冒险', cover: '/static/images/image5.jpg' },
    { id: 6, title: '三毛成长记：青春岁月', cover: '/static/images/image6.jpg' }
  ]
})

// 视频播放状态
const isPlaying = ref(false)
const videoElement = ref<HTMLVideoElement | null>(null)

// 播放视频
const playVideo = () => {
  isPlaying.value = true
}

// 暂停视频
const pauseVideo = () => {
  isPlaying.value = false
}

// 返回视频列表
const goBack = () => {
  router.push('/video')
}

// 跳转到相关视频详情
const goToRelatedVideo = (id: number) => {
  // 跳转到相关视频详情页
  router.push(`/video/${id}`)
}

onMounted(() => {
  // 根据路由参数加载对应的视频详情
  const videoId = route.params.id
  console.log('加载视频详情，ID:', videoId)
  
  // 在实际应用中，这里应该根据videoId从API获取对应的视频详情数据
  // 为简化，我们使用静态数据，但会根据ID调整内容
  if (videoId) {
    const id = parseInt(videoId as string)
    if (id >= 1 && id <= 10) {
      videoDetail.value.id = id
      videoDetail.value.title = `三毛流浪记经典片段${id}`
      videoDetail.value.cover = `/static/images/image${id}.jpg`
      videoDetail.value.videoUrl = `/static/videos/video${id}.mp4`
    }
  }
})
</script>

<template>
  <div class="video-detail-container">
    <div class="content-wrapper">
      <!-- 返回按钮 -->
      <div class="back-section">
        <button class="back-btn" @click="goBack">
          ← 返回视频列表
        </button>
      </div>
      
      <!-- 视频详情内容 -->
      <div class="video-detail-content">
        <!-- 视频封面和播放区域 -->
        <div class="video-main">
          <div class="video-cover">
            <img :src="videoDetail.cover" :alt="videoDetail.title" class="cover-image" />
            <div class="play-overlay" @click="playVideo">
              <div class="play-button">
                <span class="play-icon">▶</span>
              </div>
            </div>
          </div>
          
          <!-- 视频信息 -->
          <div class="video-info">
            <h1 class="video-title">{{ videoDetail.title }}</h1>
            <div class="video-meta">
              <span class="meta-item">{{ videoDetail.releaseDate }}</span>
              <span class="meta-item">{{ videoDetail.duration }}</span>
              <span class="meta-item rating">评分: {{ videoDetail.rating }}</span>
            </div>
            <p class="video-description">{{ videoDetail.description }}</p>
            <div class="video-details">
              <p><strong>导演:</strong> {{ videoDetail.director }}</p>
            </div>
            
            <!-- 播放按钮 -->
            <div class="play-button-container">
              <button class="play-main-btn" @click="playVideo">
                <span class="play-icon">▶</span> 立即播放
              </button>
            </div>
          </div>
        </div>
        
        <!-- 相关推荐 -->
        <div class="related-section">
          <h2 class="section-title">相关推荐</h2>
          <div class="related-grid">
            <div 
              v-for="related in videoDetail.relatedVideos" 
              :key="related.id"
              class="related-item"
              @click="goToRelatedVideo(related.id)"
            >
              <div class="related-cover">
                <img :src="related.cover" :alt="related.title" />
                <div class="related-play-overlay">
                  <div class="play-icon">▶</div>
                </div>
              </div>
              <h3 class="related-title">{{ related.title }}</h3>
            </div>
          </div>
        </div>
      </div>
    </div>
    
    <!-- 视频播放器模态框 -->
    <div v-if="isPlaying" class="video-player-modal" @click="pauseVideo">
      <div class="video-player-container" @click.stop>
        <div class="player-header">
          <h3 class="player-title">{{ videoDetail.title }}</h3>
          <button class="close-player-btn" @click="pauseVideo">×</button>
        </div>
        <video 
          ref="videoElement"
          :src="videoDetail.videoUrl" 
          controls
          autoplay
          class="video-player"
        >
          您的浏览器不支持视频播放。
        </video>
      </div>
    </div>
  </div>
</template>

<style scoped>
.video-detail-container {
  min-height: calc(100vh - 60px);
  background: #f8f8f8;
  padding: 20px;
  box-sizing: border-box;
}

.content-wrapper {
  max-width: 1200px;
  margin: 0 auto;
}

.back-section {
  margin-bottom: 20px;
}

.back-btn {
  background: #646cff;
  color: white;
  border: none;
  padding: 10px 20px;
  border-radius: 30px;
  cursor: pointer;
  font-size: 1rem;
  transition: all 0.3s ease;
}

.back-btn:hover {
  background: #535bf2;
  transform: translateY(-2px);
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.2);
}

/* 夜间模式适配 */
body.dark-mode .video-detail-container {
  background: #181a1b !important;
}

body.dark-mode .video-detail-content {
  background: #232324;
  box-shadow: 0 5px 20px rgba(0,0,0,0.7);
}

body.dark-mode .video-title,
body.dark-mode .section-title,
body.dark-mode .player-title {
  color: #fff;
}

body.dark-mode .video-meta .meta-item {
  background: #232324;
  color: #bbb;
}

body.dark-mode .rating {
  background: #535bf2;
  color: #fff;
}

body.dark-mode .video-description,
body.dark-mode .video-details {
  color: #bbb;
}

body.dark-mode .play-main-btn,
body.dark-mode .back-btn {
  background: #535bf2;
  color: #fff;
}

body.dark-mode .play-main-btn:hover,
body.dark-mode .back-btn:hover {
  background: #646cff;
}

body.dark-mode .related-item {
  background: #232324;
}

body.dark-mode .related-title {
  color: #eee;
}

body.dark-mode .related-cover {
  box-shadow: 0 5px 15px rgba(0,0,0,0.5);
}

body.dark-mode .related-play-overlay {
  background: rgba(0,0,0,0.5);
}

body.dark-mode .video-player-modal {
  background: rgba(0,0,0,0.95);
}

body.dark-mode .video-player-container {
  background: #232324;
}

body.dark-mode .player-header {
  background: #181a1b;
  border-bottom: 1px solid #333;
}

body.dark-mode .close-player-btn {
  color: #aaa;
}

body.dark-mode .close-player-btn:hover {
  background: #333;
}

.video-detail-content {
  background: white;
  border-radius: 15px;
  overflow: hidden;
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
}

.video-main {
  padding: 30px;
}

.video-cover {
  position: relative;
  width: 100%;
  max-width: 800px;
  margin: 0 auto 30px;
  border-radius: 10px;
  overflow: hidden;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
}

.cover-image {
  width: 100%;
  display: block;
  aspect-ratio: 16/9;
  object-fit: cover;
}

.play-overlay {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  opacity: 0;
  transition: opacity 0.3s ease;
}

.video-cover:hover .play-overlay {
  opacity: 1;
}

.play-button {
  width: 80px;
  height: 80px;
  border-radius: 50%;
  background: rgba(100, 108, 255, 0.9); /* 使用主题色 */
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.3s ease;
}

.play-button:hover {
  transform: scale(1.1);
  background: rgba(83, 91, 242, 0.9); /* 悬停时 slightly darker */
}

.play-icon {
  font-size: 2rem;
  color: white;
  margin-left: 5px;
}

.video-info {
  max-width: 800px;
  margin: 0 auto;
}

.video-title {
  font-size: 2rem;
  color: #333;
  margin: 0 0 15px;
  text-align: center;
}

.video-meta {
  display: flex;
  justify-content: center;
  gap: 20px;
  margin-bottom: 20px;
  flex-wrap: wrap;
}

.meta-item {
  background: #f0f0f0;
  padding: 5px 15px;
  border-radius: 20px;
  font-size: 0.9rem;
  color: #666;
}

.rating {
  background: #646cff;
  color: white;
  font-weight: bold;
}

.video-description {
  color: #555;
  line-height: 1.6;
  font-size: 1.1rem;
  text-align: center;
  margin-bottom: 20px;
}

.video-details {
  text-align: center;
  color: #666;
  margin-bottom: 20px;
}

.play-button-container {
  text-align: center;
  margin: 30px 0;
}

.play-main-btn {
  background: #646cff;
  color: white;
  border: none;
  padding: 15px 40px; /* 增加左右内边距 */
  border-radius: 30px;
  cursor: pointer;
  font-size: 1.2rem; /* 增大字体 */
  transition: all 0.3s ease;
  display: inline-flex;
  align-items: center;
  gap: 10px;
  box-shadow: 0 4px 15px rgba(100, 108, 255, 0.3); /* 添加阴影 */
}

.play-main-btn:hover {
  background: #535bf2;
  transform: translateY(-3px); /* 增加向上移动效果 */
  box-shadow: 0 7px 20px rgba(100, 108, 255, 0.4);
}

.section-title {
  font-size: 1.5rem;
  color: #333;
  text-align: center;
  margin: 30px 0 20px;
  padding-bottom: 10px;
  border-bottom: 2px solid #646cff;
}

.related-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 20px;
  padding: 0 20px 30px;
}

.related-item {
  cursor: pointer;
  transition: transform 0.3s ease;
  border-radius: 8px;
  overflow: hidden;
}

.related-item:hover {
  transform: translateY(-5px);
}

.related-cover {
  width: 100%;
  aspect-ratio: 16/9;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
  position: relative;
}

.related-cover img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
  transition: transform 0.3s ease;
}

.related-item:hover .related-cover img {
  transform: scale(1.05);
}

.related-play-overlay {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.3);
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  transition: opacity 0.3s ease;
}

.related-item:hover .related-play-overlay {
  opacity: 1;
}

.related-play-overlay .play-icon {
  font-size: 1.5rem;
  color: white;
}

.related-title {
  margin: 10px 0 0;
  color: #333;
  font-size: 1rem;
  text-align: center;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* 视频播放器模态框 */
.video-player-modal {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.9);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}

.video-player-container {
  background: white;
  border-radius: 12px;
  overflow: hidden;
  max-width: 90%;
  max-height: 90%;
  width: 1000px;
  height: 80vh;
  display: flex;
  flex-direction: column;
}

.player-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 15px 20px;
  background: #f5f5f5;
  border-bottom: 1px solid #eee;
}

.player-title {
  margin: 0;
  color: #333;
  font-size: 1.3rem;
}

.close-player-btn {
  background: none;
  border: none;
  font-size: 1.5rem;
  cursor: pointer;
  color: #666;
  padding: 0;
  width: 30px;
  height: 30px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  transition: background 0.3s ease;
}

.close-player-btn:hover {
  background: #e0e0e0;
}

.video-player {
  width: 100%;
  height: 100%;
  outline: none;
}

@media (max-width: 768px) {
  .video-main {
    padding: 20px 15px;
  }
  
  .video-title {
    font-size: 1.5rem;
  }
  
  .video-meta {
    gap: 10px;
  }
  
  .related-grid {
    grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
    gap: 15px;
    padding: 0 10px 20px;
  }
  
  .video-player-container {
    width: 95%;
    height: 70vh;
  }
  
  .play-main-btn {
    padding: 12px 24px;
    font-size: 1rem;
  }
}
</style>